<?php

namespace app\common\model\banxi\core;

use app\common\enums\AccountTypeEnum;
use app\common\enums\ClassHourTypeEnum;
use app\common\model\common\BaseModel;
use app\common\service\core\CoreAccountClassHourService;
use ba\Exception;
use think\db\exception\PDOException;
use think\exception\ValidateException;
use think\model\concern\SoftDelete;

/**
 * 账户课时表
 */
class CoreAccountClassHour extends BaseModel
{
    use SoftDelete;

    protected $name = 'banxi_core_account_class_hour';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;

    protected $append = [
        'accountTypeCn'
    ];
    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'studentId' => 'string',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s',
    ];


    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                       ｜
    //-----------------------------------------------------------------------------------

    public function getAccountTypeCnAttr(string|null $value, array $data): string
    {
        $value = $value ?: ($data['accountType'] ?? '');
        return (new AccountTypeEnum)->getDescription($value) ?? '';
    }

    //-----------------------------------------------------------------------------------
    //｜                                   处理方法                                       ｜
    //-----------------------------------------------------------------------------------


    public function getService(): CoreAccountClassHourService
    {
        return new CoreAccountClassHourService();
    }

    public function getByStudentId($studentId)
    {
        try {
            $res = $this->getService()->getByAccountIdAndType($studentId, AccountTypeEnum::STUDENT);
        } catch (Exception|PDOException|ValidateException $e) {
            throw new ValidateException($e->getMessage());
        }
        return $res;
    }

    public function classMinutes(string $classHourType, CoreAccountClassHour $accountClassHour)
    {
        switch ($classHourType) {
            case ClassHourTypeEnum::C_30:
                return $accountClassHour['class30Minute'] ?? 0;
            case ClassHourTypeEnum::C_60:
                return $accountClassHour['class60Minute'] ?? 0;
            default:
                throw new ValidateException('未知的课程类型');
        }
    }

    public function classMinutesFreeze(string $classHourType, CoreAccountClassHour $accountClassHour)
    {
        switch ($classHourType) {
            case ClassHourTypeEnum::C_30:
                return $accountClassHour['class30MinuteFreeze'] ?? 0;
            case ClassHourTypeEnum::C_60:
                return $accountClassHour['class60MinuteFreeze'] ?? 0;
            default:
                throw new ValidateException('未知的课程类型');
        }
    }

    public function classMinutesTotal($classHourType, CoreAccountClassHour $accountClassHour)
    {
        switch ($classHourType) {
            case ClassHourTypeEnum::C_30:
                return $accountClassHour['class30MinuteTotal'] ?? 0;
            case ClassHourTypeEnum::C_60:
                return $accountClassHour['class60MinuteTotal'] ?? 0;
            default:
                throw new ValidateException('ClassHourType Error');
        }
    }

    public function buyClassMinuteTotal($classHourType, CoreAccountClassHour $accountClassHour)
    {
        switch ($classHourType) {
            case ClassHourTypeEnum::C_30:
                return $accountClassHour['buyClass30MinuteTotal'] ?? 0;
            case ClassHourTypeEnum::C_60:
                return $accountClassHour['buyClass60MinuteTotal'] ?? 0;
            default:
                throw new ValidateException('ClassHourType Error');
        }
    }

    public function getListPage(array $where, string $page = "1", int $limit = 10)
    {
        return $this->where($where)->paginate([
            'page' => $page,
            'list_rows' => $limit,
        ]);
    }

    public function getInfo($where)
    {
        return $this->where($where)->find();
    }
    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------


}